package org.dubbohome;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.List;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

public class GenerateMain {
	public static void main(String[] args) throws Exception{
		 List<TableMeta> tableList ;
	        Writer out = null ;
	        String targetDir = Consts.TARGET_DIR ;
	        
	        tableList = AnalysisDB.readDB() ;
	        AnalysisDB.readTables(tableList) ;
	        // 输出到文件
	        File dir = new File(targetDir) ;
	        if(!dir.isDirectory()) {
	            dir.mkdirs() ;
	        }
	        
	        Configuration cfg = new Configuration() ;
	        cfg.setDirectoryForTemplateLoading(new File("src/main/resources/tpl")) ;
	        cfg.setObjectWrapper(new DefaultObjectWrapper()); 
//	        Template tpl = cfg.getTemplate("model.ftl") ;
//	        if(tableList != null) {
//	            for(TableMeta tm : tableList) {
//	                if(StringUtils.isBlank(tm.getClassName()))continue ;
//	                out = new FileWriter(new File(targetDir + tm.getClassName() + ".java")) ;
//	                tpl.process(tm, out) ;
//	                System.out.println("===文件 " + tm.getClassName() + ".java" + " 生成成功===");
//	            }
//	        }
	        
//	        Template tplEdit = cfg.getTemplate("edit.ftl");
//	        if(tableList != null){
//	        	for(TableMeta tm : tableList){
//	        		tm.setPageHead("<#include \"/common/common_head.ftl\">");
//	        		tm.setHideContent("<input type=\"hidden\" name=\"id\" value=\"${entity.id?string('###')}\">");
//	        		out = new FileWriter(new File(targetDir+ tm.getClassName().toLowerCase()+"_edit.ftl"));
//	        		tplEdit.process(tm, out);
//	        	}
//	        }
	        
//	        Template tplController = cfg.getTemplate("controller.ftl");
//	        if(tableList != null){
//	        	for(TableMeta tm : tableList){
//	        		out = new FileWriter(new File(targetDir+ tm.getClassName()+"Controller.java"));
//	        		tplController.process(tm, out);
//	        	}
//	        }
	        
	        Template tplService = cfg.getTemplate("service.ftl");
	        if(tableList != null){
	        	for(TableMeta tm : tableList){
	        		out = new FileWriter(new File(targetDir+ tm.getClassName()+"Service.java"));
	        		tplService.process(tm, out);
	        	}
	        }
	        
	        Template tplServiceImpl = cfg.getTemplate("serviceImpl.ftl");
	        if(tableList != null){
	        	for(TableMeta tm : tableList){
	        		out = new FileWriter(new File(targetDir+ tm.getClassName()+"ServiceImpl.java"));
	        		tplServiceImpl.process(tm, out);
	        	}
	        }
	        
	        Template tplRepository = cfg.getTemplate("repository.ftl");
	        if(tableList != null){
	        	for(TableMeta tm : tableList){
	        		out = new FileWriter(new File(targetDir+ tm.getClassName()+"Repository.java"));
	        		tplRepository.process(tm, out);
	        	}
	        }
	        
	        out.flush() ;
	        out.close() ;
	}
	
}
